from fastapi import FastAPI, HTTPException
from vo import BuildIDAGItem, TriggerDAGItem, ActivateDAGItem
from response import create_response
from dag_builder import build_dag_py
from dag_trigger import trigger_dag_impl, activate_dag_impl
from save_dag_py import save_local, save_remote
import custom_config

app = FastAPI()

@app.post("/submit/dag/")
async def submit_dag(item: BuildIDAGItem):
    if item is not None:
        res = build_dag_py(item)
        print(res)
        filename = item.user_id + "-" + "dag.py"
        await save_local(custom_config.LOCAL_DAG_PATH, res, filename)
        await save_remote(res, filename)
        return create_response(
            success=True,
            message="success",
            data={"res" : res}
        )

    return create_response(
        success=False,
        message="no item",
    )

@app.post("/trigger/dag/")
async def trigger_dag(item: TriggerDAGItem):
    if item is not None:
        res = await trigger_dag_impl(item)
        return res

    return create_response(
        success=False,
        message="no item",
    )

@app.post("/activate/dag/")
async def activate_dag(item: ActivateDAGItem):
    if item is not None:
        res = await activate_dag_impl(item)
        return res

    return create_response(
        success=False,
        message="no item",
    )